﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.Sql;
using System.Data.SqlClient;
using System.ComponentModel;

namespace DAO
{
    public class AuthorDAO
    {
        public int InsertAuthor(AuthorDTO authorDTO)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    TacGia authorInt = new TacGia
                    {
                        MaTacGia = authorDTO.AuthorCode,
                        TenTacGia = authorDTO.AuthorName,
                        SuDung = true
                    };

                    if (ent.TacGias.Count(t => t.TacGiaID != null) > 0)
                    {
                        authorInt.TacGiaID = ent.TacGias.Max(t => t.TacGiaID) + 1;
                    }
                    else
                    {
                        authorInt.TacGiaID = 1;
                    }

                    ent.AddToTacGias(authorInt);

                    DocSequenceDAO docSeqDAO = new DocSequenceDAO();
                    docSeqDAO.UpdateNextDocSequence(DocSequence.Author.ToString());

                    ent.SaveChanges();

                    return authorInt.TacGiaID;
                }
            }
            catch (Exception ex)
            {
                Utility.LogEx(ex);
                return 0;
            }
        }

        public AuthorDTO CreateAuthorDTOFromEntity(TacGia tacGia)
        {
            AuthorDTO authorDto = new AuthorDTO
            {
                AuthorID = tacGia.TacGiaID,
                AuthorCode = tacGia.MaTacGia,
                AuthorName = tacGia.TenTacGia,
                Status = bool.Parse(tacGia.SuDung.ToString())
            };
            return authorDto;
        }
        public List<AuthorDTO> GetAuthorList()
        {
            List<AuthorDTO> listAuthorDto = new List<AuthorDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<TacGia> listTacGia = ent.TacGias.Where(a => a.SuDung == true).ToList();
                foreach (TacGia item in listTacGia)
                {
                    AuthorDTO authorDto = CreateAuthorDTOFromEntity(item);
                    listAuthorDto.Add(authorDto);
                }
            }
            return listAuthorDto;
        }
        public BindingList<AuthorDTO> GetNewBindingListAuthor()
        {
            BindingList<AuthorDTO> listAuthorDto = new BindingList<AuthorDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<TacGia> listTacGia = ent.TacGias.Where(a => a.SuDung == true).ToList();
                foreach (TacGia item in listTacGia)
                {
                    AuthorDTO authorDto = CreateAuthorDTOFromEntity(item);
                    listAuthorDto.Add(authorDto);
                }
            }
            return listAuthorDto;
        }
        public bool UpdateAuthor(AuthorDTO authorDto)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    TacGia author = ent.TacGias.FirstOrDefault(c => c.TacGiaID == authorDto.AuthorID);

                    author.TenTacGia = authorDto.AuthorName;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        public AuthorDTO GetAuthorByID(int id)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    TacGia author = ent.TacGias.FirstOrDefault(c => c.TacGiaID == id);
                    AuthorDTO authDto = new AuthorDTO
                    {
                        AuthorID = author.TacGiaID,
                        AuthorCode = author.MaTacGia,
                        AuthorName = author.TenTacGia,
                        Status = bool.Parse(author.SuDung.ToString())
                    };
                    return authDto;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool CheckExistsCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int count = ent.TacGias.Where(c => c.MaTacGia == code).Count();

                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return true;
            }
        }

        public bool DeleteAuthor(int authorID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    TacGia authorEnt = ent.TacGias.FirstOrDefault(c => c.TacGiaID == authorID);
                    authorEnt.SuDung = false;
                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool CheckRelation(int authorID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int count = ent.Saches.Where(s => s.MaTacGia == authorID).Count();

                    if (count > 0)
                    {
                        return true;
                    }
                    else
                        return false;
                }
            }
            catch
            {
                return true;
            }
        }
    }
}
